package com.polysichong.netschool.dao.admin.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.polysichong.netschool.dao.admin.UserDao;
import com.polysichong.netschool.entity.admin.User;

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {


	@Override
	public void saveUser(User user) {
		this.getHibernateTemplate().save(user);
	}

	@Override
	public void deleteUser(Long id) {
		User user = getHibernateTemplate().load(User.class, id);
		if (user != null){
			this.getHibernateTemplate().delete(user);
		}
	}

	@Override
	public User findUserById(Long id) {
		return (User)this.getHibernateTemplate().get(User.class, id);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<User> findAllUsers() {
		String hql = "from User";
        return (List<User>)this.getHibernateTemplate().find(hql);
	}

	@Override
	public void updateUser(User user) {
		User u = getHibernateTemplate().load(User.class, user.getId());
		if (u != null) {
			u.setUserName(user.getUserName());
			u.setEmail(user.getEmail());
		}
		this.getHibernateTemplate().update(u);
	}

}
